var key = $("#searchAreaName");
key.bind("focus", focusKey).bind("blur", blurKey).bind("propertychange",
		searchNode).bind("input", searchNode);
var lastValue = "", nodeList = [], fontCss = {};
var setting = {
	check : {
		enable : true,
		chkboxType : {
			"Y" : "",
			"N" : ""
		}
	},
	view : {
		dblClickExpand : false,
		fontCss : getFontCss
	},
	data : {
		key : {
			title : "t"
		},
		simpleData : {
			enable : true
		}
	},
	callback : {
		beforeClick : beforeClick,
		onCheck : onCheck
	}
};

function beforeClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1");
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function onCheck(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1");
	nodes = zTree.getCheckedNodes(true);
	var v = "";
	var ids = "";
	for (var i = 0, l = nodes.length; i < l; i++) {
		v += nodes[i].name + ",";
		ids += nodes[i].id + ",";
	}
	if (v.length > 0)
		v = v.substring(0, v.length - 1);
	if (ids.length > 0)
		ids = ids.substring(0, ids.length - 1);
	$("#officeAreaIds").val(ids);
	$("#officeAreaNames").val(v);
}

function showMenu() {
	var officeAreaNames = $("#officeAreaNames");
	var cityOffset = officeAreaNames.offset();
	$("#menuContent").css({
		left : cityOffset.left + "px",
		top : cityOffset.top + officeAreaNames.outerHeight() + "px"
	}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "officeAreaNames"
			|| event.target.id == "menuContent" || $(event.target).parents(
			"#menuContent").length > 0)) {
		hideMenu();
	}
}
function focusKey(e) {
	if (key.hasClass("empty")) {
		key.removeClass("empty");
	}
}
function blurKey(e) {
	if (key.get(0).value === "") {
		key.addClass("empty");
	}
}

function clickRadio(e) {
	lastValue = "";
	searchNode(e);
}
function searchNode(e) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1");
	var value = $.trim(key.get(0).value);
	var keyType = "name";
	if (lastValue === value)
		return;
	lastValue = value;
	if (value === ""){
		updateNodes(false);
		return;
	}
	updateNodes(false);
	
	nodeList = zTree.getNodesByParamFuzzy(keyType, value);
	zTree.expandAll(true); 
	updateNodes(true);

}
function updateNodes(highlight) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1");
	for (var i = 0, l = nodeList.length; i < l; i++) {
		nodeList[i].highlight = highlight;
		zTree.updateNode(nodeList[i]);
	}
}
function getFontCss(treeId, treeNode) {
	return (!!treeNode.highlight) ? {
		color : "#A60000",
		"font-weight" : "bold"
	} : {
		color : "#333",
		"font-weight" : "normal"
	};
}
function setCheckNodeById(treeId) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1");
	var node = zTree.getNodeByParam("id", treeId);
	zTree.checkNode(node, true, false, null);
}
function checkNode(e) {
	var zTree = $.fn.zTree.getZTreeObj("tree_1"),
	type = e.data.type,
	nodes = zTree.getSelectedNodes();
	if (type.indexOf("All")<0 && nodes.length == 0) {
		alert("请先选择一个节点");
	}

	if (type == "checkAllTrue") {
		zTree.checkAllNodes(true);
	} else if (type == "checkAllFalse") {
		zTree.checkAllNodes(false);
		$("#officeAreaIds").val("");
		$("#officeAreaNames").val("");
	} else {
		var callbackFlag = $("#callbackTrigger").attr("checked");
		for (var i=0, l=nodes.length; i<l; i++) {
			if (type == "checkTrue") {
				zTree.checkNode(nodes[i], true, false, callbackFlag);
			} else if (type == "checkFalse") {
				zTree.checkNode(nodes[i], false, false, callbackFlag);
			} else if (type == "toggle") {
				zTree.checkNode(nodes[i], null, false, callbackFlag);
			}else if (type == "checkTruePS") {
				zTree.checkNode(nodes[i], true, true, callbackFlag);
			} else if (type == "checkFalsePS") {
				zTree.checkNode(nodes[i], false, true, callbackFlag);
			} else if (type == "togglePS") {
				zTree.checkNode(nodes[i], null, true, callbackFlag);
			}
		}
	}
}
